package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.se.tms.dao.Dao;
import team.se.tms.dto.Page;
import team.se.tms.dto.RoleDetail;
import team.se.tms.po.RoleInfo;
import team.se.tms.service.RoleInfoService;
import team.se.tms.util.KeyUtil;

import java.util.List;

/**
 * 角色信息业务层，具体实现角色信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/11/18
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service("RoleInfoService")
public class RoleInfoServiceImpl implements RoleInfoService {
    @Autowired
    private Dao dao;
    /**
     * 根据用户id获取角色信息
     *
     * @param userId 用户id
     * @return 角色信息
     * @throws Exception 异常
     * @create on 2018/11/17 by winkyle
     */
    @Override
    public RoleInfo getRoleInfo(String userId) throws Exception {
        return dao.selectOne("RoleInfoMapper.selectRoleInfoByUserId",userId);
    }

    /**
     * 根据用户id获取角色信息，该角色拥有的权限为该角色的属性
     *
     * @param userId 用户id
     * @return 角色信息
     * @throws Exception 异常
     * @create on 2018/11/17 by winkyle
     */
    @Override
    public RoleDetail getRoleDetail(String userId) throws Exception {
        return dao.selectOne("RoleInfoMapper.selectRoleDetailByUserId",userId);
    }

    /**
     * 添加角色
     *
     * @param roleDetail 角色信息
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/20 by johnny
     */

    @Override
    public Boolean saveRole(RoleDetail roleDetail) throws Exception {
        roleDetail.setRoleId(KeyUtil.generatePrimaryKey());
        int roleResult = dao.insert("RoleInfoMapper.insertRoleInfo",roleDetail);
//        返回0？？
        int role_AuthorityRetult = dao.insert("RoleInfoMapper.insertRole_AuthorityInfo",roleDetail);
        if(roleResult>0){
            return true;
        }
        return false;
    }
    /**
     * 分页查询角色
     *
     * @param page 分页
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/21 by johnny
     */
    @Override
    public List<RoleDetail> listAllRoleDetails(Page page) throws Exception {
        List<RoleDetail> roleDetailList =
                dao.selectList("RoleInfoMapper.selectAllRoleDetails",page);
        return roleDetailList;
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     * @return Boolean 删除结果
     * @throws Exception 异常
     * @create on 2018/12/21 by johnny
     */
    @Override
    public Boolean removeRole(String roleId) throws Exception {
        //把该角色是的用户设置为默认角色
        int updateResult = dao.update("RoleInfoMapper.updateUserToDefultRole",roleId);
        //删除角色信息
        int deleteRoleResult =dao.delete("RoleInfoMapper.deleteRoleByRoleId",roleId);
        //删除角色权限关联信息
        int deleteRoleAuthorityResult =
                dao.delete("RoleInfoMapper.deleteRole_AuthorityByRoleId",roleId);
        //可能没人用这个角色，可能该角色没有权限
        if(deleteRoleResult>0){
            return true;
        }
        return false;
    }

    /**
     * 更新角色
     *
     * @param roleDetail 修改后的角色信息
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/21 by johnny
     */

    @Override
    public Boolean modifyRole(RoleDetail roleDetail) throws Exception {
        dao.update("RoleInfoMapper.updateRoleNameByRoleDetial",roleDetail);
        String roleId =roleDetail.getRoleId();
        int a =1/0;
        dao.delete("RoleInfoMapper.deleteRole_AuthorityByRoleDetail",roleId);
        dao.insert("RoleInfoMapper.insertRole_AuthorityInfo",roleDetail);

        //获取仅包含新增权限的RoleDetail
//        RoleDetail addRoleDetail = (RoleDetail)newRoleDetail.clone();
//        addRoleDetail.getAuthorityInfos().removeAll(oldRoleDetail.getAuthorityInfos());
//        //如果用户修改了角色名，则在数据库中修改
//        if(!(oldRoleDetail.getRoleName()).equals(newRoleDetail.getRoleName())){
//            dao.update("RoleInfoMapper.updateRoleNameByRoleDetial",addRoleDetail);
//        }
//        //在数据库中插入用户新勾选的权限
//        if(null != addRoleDetail.getAuthorityInfos() && addRoleDetail.getAuthorityInfos().size()!=0){
//            dao.insert("RoleInfoMapper.insertRole_AuthorityInfo",addRoleDetail);
//        }
//        //获取用户勾选掉的权限
//        oldRoleDetail.getAuthorityInfos().removeAll(newRoleDetail.getAuthorityInfos());
//        //在数据库中删除用户勾掉的权限
//        if(null != oldRoleDetail.getAuthorityInfos() && oldRoleDetail.getAuthorityInfos().size()!=0){
//            dao.delete("RoleInfoMapper.deleteRole_AuthorityByRoleDetail",oldRoleDetail);
//        }

        return true;
    }

    /**
     * 查询所有角色信息，加载到下拉框
     * @create on 2018/12/27 by argentum
     *
     * @return 角色列表
     * @throws Exception
     */
    @Override
    public List<RoleInfo> listAllRoleInfos() throws Exception {
        List<RoleInfo> roleInfos = dao.selectList("RoleInfoMapper.selectAllRoleInfos",null);
        return roleInfos;
    }


}
